//
//  LightningUpdateViewController.m
//  LINNER
//
//  Created by wei wang on 2017/5/2.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "LightningUpdateViewController.h"
#import "Masonry.h"
#import "LightningModel.h"

@interface LightningUpdateViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *placeImage;
@property (weak, nonatomic) IBOutlet UILabel *labelone;
@property (weak, nonatomic) IBOutlet UILabel *labeltwo;

@end

@implementation LightningUpdateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setNavigationBarTitle:MyNSLocalizedString(@"固件升级")];
    [self setLeftButtonImage:[UIImage imageNamed:@"tittle_back_nor"]];
    [self.navigationController.navigationBar makeTransparent_nornav];
    
//    self.labelone.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"最新版本"),@"1.02"];//MyNSLocalizedString(@"Current Version 1.02");
//    self.labeltwo.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"当前版本"),@"1.01"];// MyNSLocalizedString(@"Current Version 1.0");
//
//
////    NSLog(@"版本信息%@",EXRACT_BOOL(THELASTESTVERSION));
//
//    NSArray *array = [LightningModel selectAllObject];
//    LightningModel *light = array.lastObject;
//    if ([light.isLastVersion isEqualToString:@"YES"])
//    {
//        self.labelone.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"最新版本"),@"1.02"];
//        self.labeltwo.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"当前版本"),@"1.02"];
//    }
    
    self.labelone.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"最新版本"),[AppDelegate globalDelegate].deviceToConnect.latestVersion];
    self.labeltwo.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"当前版本"),[AppDelegate globalDelegate].deviceToConnect.versionString];
    
//    if (EXRACT_BOOL(THELASTESTVERSION) == YES) {
//
//        self.labelone.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"最新版本"),@"1.02"];
//        self.labeltwo.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"当前版本"),@"1.02"];
//
//    }else{
//
//  //      [self setNavigationBarTitle:MyNSLocalizedString(@"固件升级")];
//
//    }
    [self setmasLayout];
    self.circle.hidden = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateprogress:) name:@"update" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateOK) name:@"updateOK" object:nil];
}

- (void)updateOK{
    
    self.placeImage.hidden = NO;
    self.placeImage.image = [UIImage imageNamed:@"bg_upgrade"];
    self.circle.hidden = YES;
    self.labelone.hidden = NO;
    self.labelone.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"最新版本"),[AppDelegate globalDelegate].deviceToConnect.latestVersion];
    self.labeltwo.text = [NSString stringWithFormat:@"%@%@",MyNSLocalizedString(@"当前版本"),[AppDelegate globalDelegate].deviceToConnect.versionString];
    self.labeltwo.hidden = YES;
    self.btn.enabled = YES;
    
    [self.btn setTitle:MyNSLocalizedString(@"OK") forState:UIControlStateNormal];
    SAVE_BOOL(YES, THELASTESTVERSION);
    NSArray *array = [LightningModel selectAllObject];
    LightningModel *light = array.lastObject;
    light.isLastVersion = @"YES";
    [light updateObject];
    
    [AppDelegate globalDelegate].deviceToConnect = [DeviceToConnect sharedController];

}

- (void)updateprogress:(NSNotification *)obc{
    
    self.btn.enabled = NO;
    self.circle.hidden = NO;
    self.placeImage.hidden = YES;
    self.titleLabel.hidden = YES;
    
    NSString *time =  [NSString stringWithFormat:@"%.0lf%%",[obc.object floatValue]*100];
    
    float value = [obc.object floatValue];
    
    [self.circle setProgressValue:value andTitle:time];
    
}


- (void)setmasLayout{
    
    UIView *superView = self.view;

    [self.circle mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.width.and.height.mas_equalTo(px1080Width(600));
        make.centerY.mas_equalTo(superView.mas_centerY).mas_offset(-60);
        make.centerX.mas_equalTo(superView.mas_centerX);
        
        
    }];

    
}


- (AnnulusView *)circle{
    
    if (!_circle) {
        
        UIView *superView = self.view;
        _circle = [[AnnulusView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
        [superView addSubview:_circle];
        
    }
    return _circle;
}



- (IBAction)updateBtn:(id)sender {
//    NSArray *array = [LightningModel selectAllObject];
//    LightningModel *light = array.lastObject;
    if ([[AppDelegate globalDelegate].deviceToConnect.versionString isEqualToString:@"1.0.3"])
    {
        [SVProgressHUD showInfoWithStatus:MyNSLocalizedString(@"最新版本")];
    }
    else
    {
        [[AppDelegate globalDelegate] update];
    }
    
//    if ([light.isLastVersion isEqualToString:@"YES"])
//    {
//        [SVProgressHUD showInfoWithStatus:MyNSLocalizedString(@"最新版本")];
//    }
//    else
//    {
//        [[AppDelegate globalDelegate] update];
//    }
    
//    if (EXRACT_BOOL(THELASTESTVERSION) == YES) {
//
//        [SVProgressHUD showInfoWithStatus:MyNSLocalizedString(@"最新版本")];
//
//    }else{
//
//        [[AppDelegate globalDelegate] update];
//    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
